package vn.com.bookstore.fragment;

import java.util.ArrayList;

import vn.com.bookstore.Config;
import vn.com.bookstore.ConnectActivity;
import vn.com.bookstore.ObjectFragment;
import vn.com.bookstore.R;
import vn.com.bookstore.activity.ChapterActivity;
import vn.com.bookstore.adapter.BookAdapter;
import vn.com.bookstore.model.Book;
import vn.com.bookstore.model.ListBook;
import vn.com.bookstore.model.Store;
import vn.com.bookstore.network.NetworkAdapter;
import vn.com.bookstore.network.NetworkAdapter.OnNetworkListener;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.LinearLayout;

public class ListBookFragment extends ObjectFragment implements OnItemClickListener, OnNetworkListener {

	protected View mNoData;
	protected GridView gridStoreBook;
	protected BookAdapter mBookAdapter;
	protected LinearLayout mLayoutLoading;

	Store store;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mParent = inflater.inflate(R.layout.fragment_list_book, null);

		findViewById();

		Bundle bundle = this.getArguments();
		if (null != bundle) {
			if (bundle.containsKey(Store.TAG))
				store = (Store) bundle.getSerializable(Store.TAG);
			if (store != null)
				getActivity().setTitle(store.getName() + "");
		}

		mBookAdapter = new BookAdapter(this.getActivity(), store);
		gridStoreBook.setAdapter(mBookAdapter);
		gridStoreBook.setOnItemClickListener(this);

		// get list of Store
		if (store != null) {
			setData(store, false);
		} else {
			store = new Store();
			store.id = 1;
			store.books = new ArrayList<Book>();
			setData(store, false);
		}

		return mParent;
	}

	@Override
	public void loadData(final boolean forceLoad) {
		setData(store, forceLoad);
	}

	public void setData(final Store store, final boolean forceLoad) {
		this.store = store;
		new Thread() {
			public void run() {
				setLoading(true);
				NetworkAdapter net = new NetworkAdapter(mApplication, mHandler, ListBookFragment.this);
				Config.NetObject obj = Config.BookStore.getUrlListBook(store.id, 1, Config.LIMIT);
				Log.e("list book", "____khac.pvan: " + obj.url);
				net.sendHttpGet(ConnectActivity.METHOD_GET_BOOK, obj.url, obj.param, forceLoad);
			};
		}.start();
	}

	@Override
	protected void findViewById() {
		super.findViewById();
		gridStoreBook = (GridView) mParent.findViewById(R.id.grid_book);
		mLayoutLoading = (LinearLayout) mParent.findViewById(R.id.layout_loading);
		mNoData = mParent.findViewById(R.id.tv_nodata);
	}

	public void setLoading(final boolean isLoading) {
		try {
			mLayoutLoading.post(new Runnable() {

				@Override
				public void run() {

					mLayoutLoading.setVisibility(View.GONE);
					if (isLoading) {
						mLayoutLoading.setVisibility(View.VISIBLE);
						mNoData.setVisibility(View.GONE);
					} else {
						refreshUI();
					}
				}
			});
		} catch (NullPointerException e) {

		}
	}

	@Override
	public void onResponse(int idMethod, boolean isSuccess, String json) {
		setLoading(false);

		switch (idMethod) {
		case ConnectActivity.METHOD_GET_BOOK:
			ArrayList<Book> book = new ArrayList<Book>(ListBook.parseListGson(json));
			store.books = new ArrayList<Book>(book);
			mBookAdapter.setData(store);
			break;
		default:
			break;
		}

		refreshUI();
	}

	@Override
	public void onItemClick(AdapterView<?> adapter, View itemStore, int position, long itemID) {
		Book book = store.books.get(position);
		Intent i = new Intent(this.getActivity(), ChapterActivity.class);
		i.putExtra(Book.TAG, book);
		startActivity(i);
	}

	public void refreshUI() {
		if (mBookAdapter.getCount() == 0) {
			gridStoreBook.setVisibility(View.GONE);
			mNoData.setVisibility(View.VISIBLE);
		} else {
			gridStoreBook.setVisibility(View.VISIBLE);
			mNoData.setVisibility(View.GONE);
		}
	}
}
